/**
 * Created by xu_jiaHui on 2017/4/30.
 */
$(function () {
    gMainDT = $("#mainDataTables").DataTable({
        ajax: {
            url: "../rds_feedback/getFeedBack4Dt",
            type: "post",
            data: function (d) {
                d.search = $('#mainDataTables').DataTable().search(this.value);
            }
        },
        columns: [{
            data: null
        }, {
            data: "feedBackContent",
            render: function (data, type, row) {
                var $div = $("<div></div>");
                $div.html(data);
                if ($div.text().length > 20) {
                    return "<p>" + $div.text().substr(0, 20) + "...</p>";
                }
                else
                    return $div.text();
            }
        }, {
            data: "feedBackCreateTime",
            render: function (data) {
                return data.substr(0, 10);
            }
        }, {
            data: "rapporteurName"
        }, {
            data: "feedBackContent",
            render: function (data, type, row) {
                return "<button class='btn btn-primary btn-xs btn-flat' onclick='$.showFeedBackContent(" + JSON.stringify(row) + ")' title='详情'><i class='glyphicon glyphicon-zoom-in'></i></button>"
            }
        }],
        dom: "<'row'<'col-md-4'l><'#mainToolbar.col-md-8'>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'><button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>";
            strToolBar += "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>";
            strToolBar += "<span class='caret'></span><span class='sr-only'>切换下拉</span></button><ul class='dropdown-menu'>";
            strToolBar += "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button></li>";
            // 下拉工具栏不处理更新事件，但今后可能需要审核等功能，替换该位置即可
            //var buttonUpdate = "<li><button href='#' class='btn btn-warning btn-sm btn-flat btn-block' onclick='$.updateOrg()'><i class='fa fa-edit'></i> 编辑</button></li>";
            // 同上方更新事件同时处理
            //gUpdate == 0 ? strToolBar : strToolBar += buttonUpdate;
            strToolBar += "</ul></div> <div class='input-group input-group-sm'><input type='text' class='form-control' name='searchText' placeholder='建议人'>";
            strToolBar += "<span class='input-group-btn'><button class='btn btn-primary btn-flat' type='button' onclick='$(this).searchDT()'><i class='fa fa-search'></i></button></span></div></div>";
            $("#mainToolbar").append(strToolBar);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    })
});
$.fn.searchDT = function () {
    // 注意gSelect查询权限的处理，由该表是否存在添加人或者审核人决定
    var searchContext = $("input[name='searchText']").val();
    gMainDT.search(searchContext).draw();
};

$.showFeedBackContent = function (data) {
    $("#showRapporteurName").html(data.rapporteurName);
    $("#feedBackTime").html(data.feedBackCreateTime.substr(0, 10));
    $("#feedBackContent").html(data.feedBackContent);
    $("#feedBack-modal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
};